package com.coffee.service.impl;

import com.coffee.common.BaseContext;
import com.coffee.dao.CategoryDAO;
import com.coffee.dao.CoffeeDAO;
import com.coffee.dao.CoffeeFlavorDAO;
import com.coffee.dto.CoffeeDto;
import com.coffee.entity.*;
import com.coffee.service.CategoryService;
import com.coffee.service.CoffeeFlavorService;
import com.coffee.service.CoffeeService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class CoffeeServiceImpl implements CoffeeService {
    @Autowired
    private CoffeeDAO coffeeDAO;

    @Autowired
    private CategoryDAO categoryDAO;

    @Autowired
    private CoffeeFlavorDAO coffeeFlavorDAO;

    @Override
    public List<Coffee> list(String id) {
        return coffeeDAO.list(id);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public PageResult page(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Coffee> coffeePage = coffeeDAO.page(queryPageBean);
        List<Coffee> coffeeList = coffeePage.getResult();
        List<CoffeeDto> coffeeDtoList = new ArrayList<>();
        for (Coffee coffee : coffeeList) {
            CoffeeDto coffeeDto = new CoffeeDto();
            BeanUtils.copyProperties(coffee,coffeeDto);
            Category category = categoryDAO.getById(coffee.getCategoryId());
            coffeeDto.setCategoryName(category.getName());
            coffeeDtoList.add(coffeeDto);
        }
        return new PageResult(coffeePage.getTotal(), coffeeDtoList);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(CoffeeDto coffeeDto) {
        Coffee coffee = new Coffee();
        BeanUtils.copyProperties(coffeeDto,coffee);
        coffee.setId(UUID.randomUUID().toString());
        coffee.setCreateTime(LocalDateTime.now());
        coffee.setCreateUser(BaseContext.get());
        coffee.setStatus(1);
        coffeeDAO.add(coffee);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(CoffeeDto coffeeDto) {
        Coffee coffee = new Coffee();
        BeanUtils.copyProperties(coffeeDto,coffee);
        coffee.setUpdateTime(LocalDateTime.now());
        String id = BaseContext.get();
        coffee.setUpdateUser(id);
        coffeeDAO.edit(coffee);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(Coffee coffee) {
        coffeeDAO.delete(coffee);
        coffeeFlavorDAO.delete(coffee.getId());
    }

    @Override
    public void status(Coffee coffee) {
        coffeeDAO.status(coffee);
    }

    /**
     * 查询所有搞活动的咖啡
     * @return
     */
    @Override
    public List<Coffee> listDisCount() {
        return coffeeDAO.listDisCount();
    }
}
